<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
	xmlns:fo="http://www.w3.org/1999/XSL/Format">
	<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
	<xsl:param name="id" select="descendant::c/@id"/>
	<xsl:param name="exist-url"/>
	<xsl:param name="url">
		<xsl:value-of select="document(concat($exist-url, '/site_info.xml'))//url"/>
	</xsl:param>
	<xsl:param name="source">ead</xsl:param>

	<xsl:param name="eadheader_url">
		<xsl:value-of select="document(concat($exist-url, '/site_info.xml'))//eadheader_url"/>
	</xsl:param>
	<xsl:param name="archdesc_did_url">
		<xsl:value-of select="document(concat($exist-url, '/site_info.xml'))//archdesc_did_url"/>
	</xsl:param>

	<xsl:param name="agency_text">
		<xsl:value-of select="document($eadheader_url)//publisher"/>
	</xsl:param>

	<!-- this has to be hard coded in because coins.lib cannot communicate with itself: -->
	<xsl:param name="image_url">
		<xsl:value-of select="document(concat($exist-url, '/site_info.xml'))//url"/>
	</xsl:param>
	<xsl:template match="/">
		<xsl:apply-templates select="descendant::c[@level='item']"/>
	</xsl:template>
	<xsl:template match="c[@level='item']">
		<fo:root>
			<fo:layout-master-set>
				<fo:simple-page-master master-name="A4-portrait" page-height="11in"
					page-width="8.5in" margin-top="1cm" margin-bottom="1cm" margin-left="1.5cm"
					margin-right="1.5cm">
					<fo:region-body margin-top="1.25cm" margin-bottom="1.5cm"/>
					<fo:region-before extent="1.25cm"/>
					<fo:region-after extent="1.5cm" margin-top="26.2cm"/>
				</fo:simple-page-master>
			</fo:layout-master-set>
			<fo:page-sequence master-reference="A4-portrait">
				<fo:static-content flow-name="xsl-region-before">
					<fo:block padding="3px" background-color="#EBEBEB" font-size="10px"
						border-right="1px solid #696969" border-bottom="1px solid #696969"
						border-left="1px solid #C8C8C8" border-top="1px solid #C8C8C8">Please cite:
							<xsl:value-of select="$url"/>display/<xsl:value-of select="$source"
							/>/<xsl:value-of select="@id"/>, <xsl:value-of
							select="document(concat($exist-url, '/site_info.xml'))/site_info/title"
						/>.</fo:block>
				</fo:static-content>
				<fo:static-content flow-name="xsl-region-after">
					<fo:block font-size="10px" font-weight="bold" text-align="center">
						<xsl:text>Page </xsl:text>
						<fo:page-number/>
					</fo:block>
				</fo:static-content>
				<fo:flow flow-name="xsl-region-body">
					<fo:block font-weight="bold" font-size="150%">
						<xsl:value-of select="normalize-space(did/unittitle)"/>
						<xsl:if test="string(did/unitdate)">
							<xsl:text>, </xsl:text>
							<xsl:value-of select="normalize-space(did/unitdate)"/>
						</xsl:if>
					</fo:block>
					<xsl:if
						test="string(did/daogrp/daoloc[@label='screen_obv']/@href) or string(did/daogrp/daoloc[@label='screen_rev']/@href)">
						<fo:block font-size="10px">
							<fo:table table-layout="fixed" width="100%">
								<fo:table-column column-width="50%"/>
								<fo:table-column column-width="50%"/>
								<fo:table-body>
									<fo:table-row>
										<fo:table-cell padding-start="3pt" padding-end="3pt"
											padding-before="3pt" padding-after="3pt">
											<fo:block>
												<fo:external-graphic width="3in"
												src="{concat($image_url, did/daogrp/daoloc[@label='screen_obv']/@href)}"
												/>
											</fo:block>
										</fo:table-cell>
										<fo:table-cell padding-start="3pt" padding-end="3pt"
											padding-before="3pt" padding-after="3pt">
											<fo:block text-align="right">
												<fo:external-graphic width="3in"
												src="{concat($image_url, did/daogrp/daoloc[@label='screen_rev']/@href)}"
												/>
											</fo:block>
										</fo:table-cell>
									</fo:table-row>
									<fo:table-row>
										<fo:table-cell padding-start="3pt" padding-end="3pt">
											<fo:block font-family="sans-serif">
												<fo:inline font-weight="bold">Obverse: </fo:inline>
												<xsl:value-of
												select="did/physdesc/physfacet[@type='obverse_legend']"/>
												<xsl:text> - </xsl:text>
												<xsl:value-of
												select="did/physdesc/physfacet[@type='obverse_iconography']"/>

											</fo:block>
										</fo:table-cell>
										<fo:table-cell padding-start="3pt" padding-end="3pt">
											<fo:block font-family="sans-serif">
												<fo:inline font-weight="bold">Reverse: </fo:inline>

												<xsl:value-of
												select="did/physdesc/physfacet[@type='reverse_legend']"/>
												<xsl:text> - </xsl:text>
												<xsl:value-of
												select="did/physdesc/physfacet[@type='reverse_iconography']"/>

											</fo:block>
										</fo:table-cell>
									</fo:table-row>
								</fo:table-body>
							</fo:table>
						</fo:block>
					</xsl:if>
					<xsl:apply-templates select="did"/>
					<xsl:call-template name="archival_data"/>
					<xsl:if
						test="custodhist or acqinfo or ancestor::archdesc/acqinfo or ancestor::c[@level='series']/acqinfo or ancestor::archdesc/custodhist or ancestor::c[@level='series']/custodhist or appraisal">
						<xsl:call-template name="admininfo"/>
					</xsl:if>
					<xsl:if test="bibliography">
						<xsl:apply-templates select="bibliography"/>
					</xsl:if>
					<xsl:if test="abstract">
						<xsl:apply-templates select="abstract"/>
					</xsl:if>
					<xsl:if test="descgrp or bioghist">
						<xsl:apply-templates select="descgrp | bioghist"/>
					</xsl:if>
					<xsl:if
						test="ancestor::node()[@level='series']/descgrp or ancestor::node()[@level='series']/bioghist">
						<xsl:apply-templates
							select="ancestor::node()[@level='series']/descgrp | ancestor::node()[@level='series']/bioghist"
						/>
					</xsl:if>
				</fo:flow>
			</fo:page-sequence>

		</fo:root>
	</xsl:template>

	<!-- DID -->

	<xsl:template match="did">
		<fo:block space-after="12pt" text-align="center">
			<fo:leader rule-thickness="1px" leader-length="5in" leader-pattern="rule"
				color="#696969"/>
		</fo:block>
		<fo:block font-size="12px" font-weight="bold">
			<xsl:text>Descriptive Information</xsl:text>
		</fo:block>
		<fo:table table-layout="fixed">
			<fo:table-column column-width="50%"/>
			<fo:table-column column-width="50%"/>
			<fo:table-body>
				<fo:table-row>
					<fo:table-cell padding-end="3pt" padding-after="3pt" padding-left="10px"
						padding-right="10px">

						<xsl:if test="string(unittitle/persname)">
							<fo:block font-size="10px">
								<xsl:for-each select="unittitle/persname">
									<xsl:choose>
										<xsl:when test="@role">
											<fo:inline font-weight="bold">
												<xsl:value-of
												select="concat(translate(substring(@role, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring(@role, 2))"/>
												<xsl:text>: </xsl:text>
											</fo:inline>
										</xsl:when>
										<xsl:otherwise>
											<fo:inline font-weight="bold">Name: </fo:inline>
										</xsl:otherwise>
									</xsl:choose>

									<xsl:value-of select="."/>
									<xsl:if test="@normal and not(@normal='anonymous')">
											(<xsl:value-of select="@normal"/>) </xsl:if>

								</xsl:for-each>
							</fo:block>
						</xsl:if>
						<xsl:if test="string(unitdate)">
							<fo:block font-size="10px">
								<fo:inline font-weight="bold">Date: </fo:inline>

								<xsl:value-of select="unitdate"/>

							</fo:block>
						</xsl:if>
						<xsl:if test="origination">
							<fo:block font-size="10px">
								<fo:inline font-weight="bold">Mint: </fo:inline>
								<xsl:apply-templates select="origination"/>
							</fo:block>
						</xsl:if>
						<xsl:if test="string(materialspec[@type='department'][1])">
							<fo:block font-size="10px">
								<xsl:choose>
									<xsl:when test="materialspec[@type='department'][2]">
										<fo:inline font-weight="bold">Departments: </fo:inline>
										<xsl:for-each select="materialspec[@type='department']">
											<xsl:value-of select="."/>
											<xsl:if test="not(position() = last())">
												<xsl:text>, </xsl:text>
											</xsl:if>
										</xsl:for-each>

									</xsl:when>
									<xsl:otherwise>
										<fo:inline font-weight="bold">Department: </fo:inline>
										<xsl:value-of
											select="normalize-space(materialspec[@type='department'])"
										/>
									</xsl:otherwise>
								</xsl:choose>
							</fo:block>
						</xsl:if>
						<xsl:if test="string(materialspec[@type='script'])">
							<fo:block font-size="10px">
								<xsl:for-each select="descendant::materialspec[@type='script']">
									<xsl:value-of select="text()"/>
									<xsl:if
										test="position() &gt; 0 and not(position() = last())">
										<xsl:text>, </xsl:text>
									</xsl:if>
								</xsl:for-each>
							</fo:block>
						</xsl:if>
					</fo:table-cell>
					<fo:table-cell padding-end="3pt" padding-before="3pt" padding-after="3pt"
						padding-right="10px" padding-left="30px">
						<xsl:if test="physdesc">
							<xsl:apply-templates select="physdesc"/>
						</xsl:if>
					</fo:table-cell>
				</fo:table-row>
			</fo:table-body>
		</fo:table>
	</xsl:template>

	<xsl:template match="origination">
		<xsl:for-each select="geogname">
			<xsl:value-of select="."/>
			<xsl:if test="@role">
				<fo:inline font-style="italic">
					<xsl:text> </xsl:text>
					<xsl:choose>
						<xsl:when test="@role='state'">(State)</xsl:when>
						<xsl:when test="@role='region'">(Region)</xsl:when>
						<xsl:when test="@role='city'">(City)</xsl:when>
						<xsl:otherwise>(Location)</xsl:otherwise>
					</xsl:choose>
				</fo:inline>
			</xsl:if>
			<xsl:if test="not(position() = last())">
				<xsl:text>, </xsl:text>
			</xsl:if>
		</xsl:for-each>
	</xsl:template>

	<xsl:template match="physdesc">
		<xsl:if test="string(genreform[@type='format'])">
			<fo:block font-size="10px">
				<fo:inline font-weight="bold">Format: </fo:inline>
				<xsl:value-of select="normalize-space(genreform[@type='format'])"/>
			</fo:block>
		</xsl:if>
		<xsl:if test="string(genreform[@type='denomination'])">
			<fo:block font-size="10px">
				<fo:inline font-weight="bold">Denomination: </fo:inline>

				<xsl:for-each select="genreform[@type='denomination']">
					<xsl:value-of
						select="concat(translate(substring(., 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring(., 2))"/>
					<xsl:if test="position() &gt; 0 and not(position() = last())">
						<xsl:text>, </xsl:text>
					</xsl:if>
				</xsl:for-each>

			</fo:block>
		</xsl:if>
		<xsl:if test="string(physfacet[@type='material'])">
			<fo:block font-size="10px">
				<fo:inline font-weight="bold">Material: </fo:inline>

				<xsl:for-each select="physfacet[@type='material']">
					<xsl:value-of
						select="concat(translate(substring(., 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring(., 2))"/>

					<xsl:if test="position() &gt; 0 and not(position() = last())">
						<xsl:text>, </xsl:text>
					</xsl:if>
				</xsl:for-each>

			</fo:block>
		</xsl:if>
		<xsl:if test="string(physfacet[@type='weight'])">
			<fo:block font-size="10px">
				<fo:inline font-weight="bold">Weight: </fo:inline>

				<xsl:value-of select="physfacet[@type='weight']"/>
				<xsl:choose>
					<xsl:when test="physfacet[@type='weight']/@unit">
						<xsl:text> </xsl:text>
						<xsl:value-of select="physfacet[@type='weight']/@unit"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text> grams</xsl:text>
					</xsl:otherwise>
				</xsl:choose>

			</fo:block>
		</xsl:if>
		<xsl:if test="string(dimensions)">
			<fo:block font-size="10px">
				<fo:inline font-weight="bold">Dimensions: </fo:inline>

				<xsl:value-of select="dimensions"/>
				<xsl:choose>
					<xsl:when test="dimensions/@unit">
						<xsl:text> </xsl:text>
						<xsl:value-of select="dimensions/@unit"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text> mm</xsl:text>
					</xsl:otherwise>
				</xsl:choose>

			</fo:block>
		</xsl:if>
		<xsl:if test="string(function)">
			<fo:block font-size="10px">
				<fo:inline font-weight="bold">Function: </fo:inline>

				<xsl:for-each select="function">
					<xsl:value-of select="."/>
					<xsl:if test="position() &gt; 0 and not(position() = last())">
						<xsl:text>, </xsl:text>
					</xsl:if>
				</xsl:for-each>

			</fo:block>
		</xsl:if>
		<xsl:if test="string(physfacet[@type='axis'])">
			<fo:block font-size="10px">
				<fo:inline font-weight="bold">Die Axis: </fo:inline>

				<xsl:value-of select="physfacet[@type='axis']"/>

			</fo:block>
		</xsl:if>

	</xsl:template>

	<xsl:template name="archival_data">
		<fo:block space-after="12pt" text-align="center">
			<fo:leader rule-thickness="1px" leader-length="5in" leader-pattern="rule"
				color="#696969"/>
		</fo:block>
		<fo:block font-size="12px" font-weight="bold">
			<xsl:text>Archival Data</xsl:text>
		</fo:block>
		<fo:block font-size="10px" margin-left="10px">
			<fo:block>
				<fo:inline font-weight="bold">Institution: </fo:inline>
				<xsl:value-of select="$agency_text"/>
			</fo:block>
			<xsl:if test="ancestor::archdesc/did/repository and ancestor::archdesc/did/physloc">

				<fo:block>
					<fo:inline font-weight="bold">Intellectual Caretaker: </fo:inline>

					<xsl:value-of select="ancestor::archdesc/did/repository"/>

				</fo:block>


				<fo:block>
					<fo:inline font-weight="bold">Physical Custodian: </fo:inline>

					<xsl:value-of select="ancestor::archdesc/did/physloc"/>

				</fo:block>
			</xsl:if>
			<xsl:if test="ancestor::archdesc/did/repository and not(ancestor::archdesc/did/physloc)">
				<fo:block>
					<fo:inline font-weight="bold">Repository: </fo:inline>

					<xsl:value-of select="ancestor::archdesc/did/repository"/>

				</fo:block>
			</xsl:if>
			<fo:block>
				<fo:inline font-weight="bold">Collection: </fo:inline>

				<xsl:value-of select="document($archdesc_did_url)//unittitle"/>

			</fo:block>
			<xsl:if test="string(did/container)">
				<fo:block>
					<fo:inline font-weight="bold">Container: </fo:inline>

					<xsl:value-of select="did/container/@type"/>
					<xsl:text> </xsl:text>
					<xsl:value-of select="did/container"/>

				</fo:block>
			</xsl:if>
			<xsl:if test="string(did/unitid)">
				<fo:block>
					<fo:inline font-weight="bold">Accession Number: </fo:inline>

					<xsl:apply-templates select="did/unitid"/>

				</fo:block>
			</xsl:if>
			<xsl:if test="string(did/physloc)">
				<fo:block>
					<fo:inline font-weight="bold">Physical location: </fo:inline>

					<xsl:apply-templates select="did/physloc"/>

				</fo:block>
			</xsl:if>
		</fo:block>
	</xsl:template>

	<xsl:template name="admininfo">
		<fo:block space-after="12pt" text-align="center">
			<fo:leader rule-thickness="1px" leader-length="5in" leader-pattern="rule"
				color="#696969"/>
		</fo:block>
		<fo:block font-size="12px" font-weight="bold">
			<xsl:text>Administrative History</xsl:text>
		</fo:block>
		<fo:block font-size="10px" margin-left="10px">
			<xsl:apply-templates select="custodhist"/>
			<xsl:apply-templates select="ancestor::c[@level='series']/custodhist"/>
			<xsl:apply-templates select="ancestor::archdesc/custodhist"/>
			<xsl:apply-templates select="acqinfo"/>
			<xsl:apply-templates select="ancestor::c[@level='series']/acqinfo"/>
			<xsl:apply-templates select="ancestor::archdesc/acqinfo"/>
		</fo:block>
	</xsl:template>

	<xsl:template match="custodhist">
		<xsl:choose>
			<xsl:when
				test="not(parent::c[@level='series'] or parent::archdesc) and not(ancestor::archdesc/custodhist or ancestor::c[@level='series']/custodhist)">
				<fo:block>
					<fo:inline font-weight="bold">Provenance: </fo:inline>
					<xsl:apply-templates/>
				</fo:block>
			</xsl:when>
			<xsl:when test="ancestor::c[@type='level'] or ancestor::archdesc">
				<xsl:choose>
					<xsl:when test="parent::c[@level='item']">
						<fo:block>
							<fo:inline font-weight="bold">Provenance (Item): </fo:inline>
							<xsl:apply-templates/>
						</fo:block>
					</xsl:when>
					<xsl:when test="parent::c[@level='series']">
						<fo:block>
							<fo:inline font-weight="bold">Provenance (Collection): </fo:inline>
							<xsl:apply-templates/>
						</fo:block>
					</xsl:when>
					<xsl:when test="parent::archdesc">
						<fo:block>
							<fo:inline font-weight="bold">Provenance (Institutional Repository): </fo:inline>
							<xsl:apply-templates/>
						</fo:block>
					</xsl:when>
				</xsl:choose>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="acqinfo">
		<xsl:choose>
			<xsl:when
				test="not(parent::c[@level='series'] or parent::archdesc) and not(ancestor::archdesc/acqinfo or ancestor::c[@level='series']/acqinfo)">
				<fo:block>
					<fo:inline font-weight="bold">Acquisition Information: </fo:inline>
					<xsl:apply-templates/>
				</fo:block>
			</xsl:when>
			<xsl:when test="ancestor::c[@type='level'] or ancestor::archdesc">
				<xsl:choose>
					<xsl:when test="parent::c[@level='item']">
						<fo:block>
							<fo:inline font-weight="bold">Acquisition Information (Item): </fo:inline>
							<xsl:apply-templates/>
						</fo:block>
					</xsl:when>
					<xsl:when test="parent::c[@level='series']">
						<fo:block>
							<fo:inline font-weight="bold">Acquisition Information (Collection): </fo:inline>
							<xsl:apply-templates/>
						</fo:block>
					</xsl:when>
					<xsl:when test="parent::archdesc">
						<fo:block>
							<fo:inline font-weight="bold">Acquisition Information (Institutional
								Repository): </fo:inline>
							<xsl:apply-templates/>
						</fo:block>
					</xsl:when>
				</xsl:choose>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="bibliography">
		<xsl:choose>
			<xsl:when test="parent::c">
				<fo:block space-after="12pt" text-align="center">
					<fo:leader rule-thickness="1px" leader-length="5in" leader-pattern="rule"
						color="#696969"/>
				</fo:block>
				<fo:block font-size="12px" font-weight="bold">
					<xsl:text>Bibliography</xsl:text>
				</fo:block>
				<fo:block font-size="10px" margin-left="20px">
					<xsl:apply-templates/>
				</fo:block>
			</xsl:when>
			<xsl:otherwise>
				<fo:block font-size="10px">
					<xsl:apply-templates/>
				</fo:block>
			</xsl:otherwise>
		</xsl:choose>

	</xsl:template>

	<xsl:template match="bibref">
		<xsl:choose>
			<xsl:when test="parent::node()/parent::c">
				<fo:block text-indent="-10px">
					<xsl:apply-templates/>
				</fo:block>
			</xsl:when>
			<xsl:otherwise>
				<fo:block text-indent="-.5in" space-after="12pt" margin-left=".5in">
					<xsl:apply-templates/>
				</fo:block>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="title">
		<xsl:choose>
			<xsl:when test="@render">
				<fo:inline font-style="{@render}">
					<xsl:value-of select="."/>
				</fo:inline>
			</xsl:when>
			<xsl:otherwise>
				<fo:inline>
					<xsl:value-of select="."/>
				</fo:inline>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="bibref/num">
		<xsl:text>no. </xsl:text>
		<xsl:value-of select="."/>
		<xsl:text>.</xsl:text>
	</xsl:template>

	<xsl:template match="abstract">
		<fo:block break-before="page">
			<fo:block font-weight="bold" font-size="150%" text-align="center" space-after="12pt">
				<xsl:choose>
					<xsl:when test="string(normalize-space(@label))">
						<xsl:value-of select="@label"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>Abstract</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</fo:block>
			<fo:block space-after="12pt" text-indent=".5in">
				<xsl:apply-templates/>
			</fo:block>
		</fo:block>
	</xsl:template>

	<xsl:template match="bioghist | descgrp">
		<fo:block break-before="page">
			<xsl:apply-templates
				select="head | scopecontent | bibliography | odd[@type='doc_author']"/>
			<xsl:if test="string(normalize-space(odd[@type='doc_author'])) and ancestor::c">
				<fo:block font-size="10pt" text-align="center" space-after="12pt"> Copyright ©2008
						<xsl:value-of select="odd[@type='doc_author']"/>. </fo:block>
			</xsl:if>
		</fo:block>
	</xsl:template>

	<xsl:template match="head">
		<xsl:choose>
			<xsl:when test="parent::descgrp or parent::bioghist">
				<fo:block font-weight="bold" font-size="150%" text-align="center" space-after="12pt">
					<xsl:value-of select="."/>
				</fo:block>
			</xsl:when>
			<xsl:when test="parent::scopecontent or parent::bibliography">
				<fo:block font-weight="bold" font-size="125%">
					<xsl:value-of select="."/>
				</fo:block>
			</xsl:when>
			<xsl:when test="parent::table">
				<fo:block font-weight="bold" font-size="110%">
					<xsl:value-of select="."/>
				</fo:block>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="odd[@type='doc_author']">
		<fo:block font-weight="bold" font-size="125%" space-after="12pt" text-align="center">
			<xsl:value-of select="."/>
		</fo:block>
	</xsl:template>

	<xsl:template match="scopecontent">
		<fo:block space-after="12pt" font-size="10px">
			<xsl:apply-templates/>
		</fo:block>
	</xsl:template>

	<xsl:template match="p">
		<xsl:choose>
			<xsl:when test="parent::scopecontent">
				<fo:block space-after="12pt" text-indent=".5in">
					<xsl:apply-templates/>
				</fo:block>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="listhead">
		<fo:block font-weight="bold">
			<xsl:apply-templates/>
		</fo:block>
	</xsl:template>

	<xsl:template match="emph">
		<xsl:choose>
			<xsl:when test="@render='italic'">
				<fo:inline font-style="italic">
					<xsl:apply-templates/>
				</fo:inline>
			</xsl:when>
			<xsl:when test="@render='bold'">
				<fo:inline font-weight="bold">
					<xsl:apply-templates/>
				</fo:inline>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="ptr">
		<xsl:variable name="current_target" select="@target"/>

		<fo:inline baseline-shift="-5px" font-size="9px">
			<!--<xsl:value-of select="@target"/>-->
			<xsl:choose>
				<xsl:when test="ancestor::descgrp">
					<xsl:for-each select="ancestor::descgrp/descendant::ptr">
						<xsl:if test="@target = $current_target">
							<xsl:value-of select="position()"/>
						</xsl:if>
					</xsl:for-each>
				</xsl:when>
				<xsl:when test="ancestor::bioghist">
					<xsl:for-each select="ancestor::bioghist/descendant::ptr">
						<xsl:if test="@target = $current_target">
							<xsl:value-of select="position()"/>
						</xsl:if>
					</xsl:for-each>
				</xsl:when>
			</xsl:choose>
		</fo:inline>
	</xsl:template>

	<xsl:template match="ref">
		<fo:inline baseline-shift="-5px" font-size="9px">
			<xsl:apply-templates/>
		</fo:inline>
	</xsl:template>

	<xsl:template match="note">
		<xsl:variable name="current_id" select="@id"/>
		<fo:block space-after="6pt">
			<fo:inline baseline-shift="-5px" font-size="9px">
				<xsl:choose>
					<xsl:when test="ancestor::descgrp">
						<xsl:for-each select="ancestor::descgrp/descendant::ptr">
							<xsl:if test="@target = $current_id">
								<xsl:value-of select="position()"/>
							</xsl:if>
						</xsl:for-each>
					</xsl:when>
					<xsl:when test="ancestor::bioghist">
						<xsl:for-each select="ancestor::bioghist/descendant::ptr">
							<xsl:if test="@target = $current_id">
								<xsl:value-of select="position()"/>
							</xsl:if>
						</xsl:for-each>
					</xsl:when>
				</xsl:choose>
			</fo:inline>
			<xsl:apply-templates/>
		</fo:block>
	</xsl:template>

	<xsl:template match="extptr">
		<fo:table table-layout="fixed" margin-left="auto" margin-right="auto" space-after="12pt">
			<fo:table-column column-width="100%"/>
			<fo:table-body>
				<fo:table-row>
					<fo:table-cell display-align="center" text-align="center">
						<fo:block>
							<fo:external-graphic src="{concat($image_url, @href)}"/>
						</fo:block>
					</fo:table-cell>
				</fo:table-row>
				<fo:table-row>
					<fo:table-cell display-align="center" text-align="center">
						<fo:block font-size="10px" font-style="italic">
							<xsl:value-of select="@title"/>
						</fo:block>
					</fo:table-cell>
				</fo:table-row>
			</fo:table-body>
		</fo:table>
	</xsl:template>

	<xsl:template match="table">
		<fo:block space-after="12pt">
			<xsl:apply-templates select="head | tgroup"/>
		</fo:block>
	</xsl:template>

	<xsl:template match="tgroup">
		<xsl:variable name="width" select="100 div @cols"/>
		<fo:table>
			<xsl:for-each select="tbody/row[1]/entry">
				<fo:table-column column-width="{$width}%"/>
			</xsl:for-each>

			<xsl:apply-templates select="thead"/>
			<xsl:apply-templates select="tbody"/>



		</fo:table>
	</xsl:template>

	<xsl:template match="thead">
		<fo:table-header>
			<xsl:apply-templates select="row"/>
		</fo:table-header>
	</xsl:template>

	<xsl:template match="tbody">
		<fo:table-body>
			<xsl:apply-templates select="row"/>
		</fo:table-body>
	</xsl:template>

	<xsl:template match="row">
		<fo:table-row>
			<xsl:apply-templates select="entry"/>
		</fo:table-row>
	</xsl:template>

	<xsl:template match="entry">
		<fo:table-cell>
			<xsl:choose>
				<xsl:when test="ancestor::thead">
					<fo:block font-weight="bold">
						<xsl:value-of select="."/>
					</fo:block>
				</xsl:when>
				<xsl:when test="ancestor::tbody">
					<fo:block>
						<xsl:value-of select="."/>
					</fo:block>
				</xsl:when>
			</xsl:choose>
		</fo:table-cell>
	</xsl:template>

</xsl:stylesheet>
